﻿# coding: utf-8

import ConfigParser
import os
import wx

import FileSystem

class GlobalSettingBase:
    def Save(self, conf):
        pass
    
    def Load(self, conf):
        pass
        
    def Apply(self):
        pass
        
class GlobalSettingGeneral(GlobalSettingBase):
    def __init__(self):
        self._workDir = os.getcwd()
        self._perspective = ''
        
    def Save(self, conf):
        conf.add_section('General')
        conf.set('General', 'workDir', self._workDir)
        conf.set('General', 'perspective', self._perspective)
        
    def Load(self, conf):
        self._workDir = conf.get('General', 'workDir').strip()
        self._perspective = conf.get('General', 'perspective').strip()
        
    def Apply(self):
        FileSystem.SetRoot(self._workDir)
        
class GlobalSetting:
    def __init__(self):
        self._general = GlobalSettingGeneral()
        
    def Save(self):
        try:
            conf = ConfigParser.ConfigParser()
            
            self._general.Save(conf)
            
            conf.write(open('config.ini', 'w'))
            
            return True
        
        except:
            SHOW_TRACE()
            return False
        
    def Load(self):
        try:
            conf = ConfigParser.ConfigParser()
            conf.read('config.ini')
            
            self._general.Load(conf)
            
            return True
            
        except:
            return False
            
    def Apply(self):
        try:
            self._general.Apply()
            
            return True
        except:
            return False
        
__builtins__['GLOBAL_SETTING'] = GlobalSetting()

def __RemoveRootPath(path):
    return path[len(GLOBAL_SETTING._general._workDir):]
        
__builtins__['REMOVE_ROOT_PATH'] = __RemoveRootPath